/**
 * Created by benz on 2017/3/19.
 */
module.exports = app => {
    return class MenuController extends app.Controller {
        * index(ctx) {

            const params=ctx.query || "";
            const pagination=params.pagination || "";//分页内容
            const pageNum=pagination.pageNum || 1; //页码
            const pageLength=pagination.pageLength || 12;//每页条数
            const screen=params.screen || "";//条件
            const orderby=params.orderby || "";//排序
            const offesrNum=(pageNum-1)*pageLength;//偏移量

            const menus = yield this.ctx.model.Menu.findAndCountAll({
                limit:pageLength,offset:offesrNum,where:screen,order:orderby
            });
            ctx.body={
                menus:menus.rows,
                params:{
                    pagination:{
                        pageNum:pageNum,
                        pageTotal:menus.count,
                        pageLength:pageLength
                    },
                    orderby:orderby,
                    screen:screen
                }
            };
        }
        * create(ctx) {
            const body=ctx.request.body;
            const menus=yield  this.ctx.model.Menu.create({
                name:body.name,title:body.title,path:body.path,type:body.type
            });
            if (menus){
                ctx.body={
                    menus:menus
                }
            }
        }
        * show(ctx){
            const menus=yield this.ctx.model.Menu.findById(ctx.params.id);
            this.ctx.body=article;
        }
        * update(ctx) {
            const body=ctx.request.body;
            const menus=yield  this.ctx.model.Menu.update({
                name:body.name,title:body.title,path:body.path,type:body.type
            },{
                where:{id:body.id}
            });
        }
        * destory(ctx) {
            const menus=yield  this.ctx.model.Menu.destory({
                where:{id:ctx.params.id}
            });
        }
    }
}